package com.xiajiucai.baiping;

import android.Manifest;
import android.content.Context;
import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.content.pm.PackageManager;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.ActivityCompat;
import androidx.core.content.ContextCompat;

import com.bumptech.glide.Glide;
import com.xiajiucai.baiping.util.GlideLoadEngine;
import com.zhihu.matisse.Matisse;
import com.zhihu.matisse.MimeType;
import com.zhihu.matisse.filter.Filter;
import com.zhihu.matisse.internal.entity.CaptureStrategy;
import com.zhihu.matisse.internal.entity.IncapableCause;
import com.zhihu.matisse.internal.entity.Item;

import java.io.FileNotFoundException;
import java.io.InputStream;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

public class CameraActivity extends AppCompatActivity {

  private TextView textView;
  private ImageView imageView;
  final int REQUEST_CODE = 6;

  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_camera);

    textView = findViewById(R.id.pz);
    imageView = findViewById(R.id.img);

    initPermission();

    textView.setOnClickListener(new View.OnClickListener() {
      @Override public void onClick(View v) {
        selectPic();
      }
    });
  }

  private void initPermission() {
    if (
        ContextCompat.checkSelfPermission(this, Manifest.permission.READ_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED
            || ContextCompat.checkSelfPermission(this, Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED
            || ContextCompat.checkSelfPermission(this, Manifest.permission.CAMERA) != PackageManager.PERMISSION_GRANTED) {
      ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE,
          Manifest.permission.WRITE_EXTERNAL_STORAGE,
          Manifest.permission.CAMERA}, REQUEST_CODE);
    }
  }

  private final int REQUEST_CODE_CHOOSE_PHOTO_ALBUM = 1;
  @Override
  protected void onActivityResult(int requestCode, int resultCode, Intent data) {

    super.onActivityResult(requestCode, resultCode, data);
    if (requestCode == REQUEST_CODE_CHOOSE_PHOTO_ALBUM && resultCode == RESULT_OK) {
      //图片路径 同样视频地址也是这个 根据requestCode
      List<Uri> pathList = Matisse.obtainResult(data);
      for (Uri _Uri : pathList) {
        Glide.with(this).load(_Uri).into(imageView);
        System.out.println(_Uri.getPath());
      }
    }
  }


  void selectPic()
  {
    Matisse.from(this)
        .choose(MimeType.ofImage(), false)
        .capture(true)  // 使用相机，和 captureStrategy 一起使用
        .captureStrategy(new CaptureStrategy(true, "com.ijovo.test.fileprovider"))
        .theme(R.style.Matisse_Dracula)
        .countable(true)
        .maxSelectable(1)
        .addFilter(new Filter() {
          @Override
          protected Set<MimeType> constraintTypes() {
            return new HashSet<MimeType>() {{
              add(MimeType.PNG);
            }};
          }

          @Override
          public IncapableCause filter(Context context, Item item) {
            try {
              InputStream inputStream = getContentResolver().openInputStream(item.getContentUri());
              BitmapFactory.Options options = new BitmapFactory.Options();
              options.inJustDecodeBounds = true;
              BitmapFactory.decodeStream(inputStream, null, options);
              int width = options.outWidth;
              int height = options.outHeight;
            } catch (FileNotFoundException e) {
              e.printStackTrace();
            }
            return null;
          }
        })
        .restrictOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT)
        .thumbnailScale(0.87f)
        .imageEngine(new GlideLoadEngine())
        .forResult(REQUEST_CODE_CHOOSE_PHOTO_ALBUM);
  }
}